

****************** Analysis *************************
/*
The analysis consist of the main Tables and Tables and figures for the Appendix. 
In some of the Appendix figures we use other data which is loaded in a separate section below
*/


clear all
cd "C:\Users\andrkot\Dropbox (Frischsenteret)\Hungarian peers\Replication files AJS\Data"
use Data\Replication_ajs.dta, clear





****************** Main Tables *************************



**** Table 1 ***************


local varlist /// 
dm_roma ///
roma_friend lend ///
age_years  girl b_mat_c  b_gra_c  b_lit_c  b_dil_c  b_beh_c ///
b_gpa same_s  ///
share_roma_class ///

cd "$results"
estpost summarize `varlist' if dm_roma!=. & roma==0
est save "desk", replace
eststo desk
cd "$results"
estpost summarize `varlist' if dm_roma==1 & roma==0
est save "desk_t", replace
eststo desk_t
estpost summarize `varlist' if dm_roma==0 & roma==0
est save "desk_c", replace
eststo desk_c



local varlist /// 
roma_friend lend ///
dm_roma ///
age_years  girl b_mat_c  b_gra_c  b_lit_c  b_dil_c  b_beh_c ///
b_gpa same_s share_roma_class ///


cd "$results"
#delimit;
esttab desk desk_t desk_c using "desk_dep.tex",
    style(tex) /*stats(N)*/ booktabs nonumbers
    cells("mean (fmt(2) pattern(1 1 1 1 1 1)) sd (fmt(2) par pattern(1 1 1 1 1 1)) ") 
		mgroups(
		"Total" "\shortstack{Treated\\Roma deskmate}" "\shortstack{Control\\Non-Roma deskmate}" 
		, pattern(1 1 1) 
		prefix(\multicolumn{@span}{c}{) 
		suffix(}) 
		span 
		erepeat(\cmidrule(lr){@span})
	)
	order(`varlist')
    collabels(\multicolumn{1}{c}{Mean} \multicolumn{1}{c}{SD})
	varlabels(
		dm_roma "Roma deskmate"
		roma_friend "Roma friend"
		lend "Lend to classmate"
	age_years "Age (in years)"
	girl  "Girl"
	roma "Roma"
	b_mat_c "Grade mathematics" 
	b_gra_c "Grade grammar" 
	b_lit_c "Grade literature"
	b_dil_c "Grade diligence"
	b_beh_c "Grade behavior"
	b_gpa "GPA"
	lend_roma "Lend to Roma"
	lend_non_roma "Lend to non-Roma"
	number_roma_f "Number of Roma friends"
	lend_dm "Lend to Deskmate"
	liked_dm "Liked sitting next to Deskmate" 
	dm_best "Deskmate among best friends"
	same_s "Deskmate of same sex"
	share_roma_class "Share of Roma in class"
	,blist(roma_friend "\multicolumn{5}{l}{\emph{Main dependent variables}} \\" 
					number_roma_f "\multicolumn{5}{l}{\emph{Other outcome variables}} \\"
					dm_roma "\multicolumn{5}{l}{\emph{Main exposure variable}} \\"
								age_years "\multicolumn{5}{l}{\emph{Control variables}} \\"
				b_gpa "\multicolumn{5}{l}{\emph{Variables for heterogeneity analysis}} \\"
	)
	)
    prehead("\begin{tabular}{l*{12}{S[table-format=4.4]}}" "\toprule") posthead(\midrule)
    postfoot("\bottomrule" "\end{tabular}")
    replace;
#delimit cr



* Test of balance:
/*
To test for balance we will regress our main treatment variable on the control variables described above both individually and together,
 while controlling for class fixed effects. We will judge whether the randomization worked by conducting an 
 F-test of whether the control variables jointly predict treatment status.
*/
**** Table 2 ***************


foreach var of varlist age_01_years girl  b_mat_c  b_gra_c  b_lit_c  b_dil_c  b_beh_c {
areg   dm_roma  `var' if roma==0, absorb(class_id) 
su dm_roma if e(sample)==1
estadd scalar mean_depvar_c = r(mean)
 estadd local Class "Yes"
 est store bal_`var'
}


areg   dm_roma  age_01_years girl  b_mat_c  b_gra_c  b_lit_c  b_dil_c  b_beh_c   if roma==0, absorb(class_id) 
su dm_roma if e(sample)==1
estadd scalar mean_depvar_c = r(mean)
 estadd local Class "Yes"
 est store bal_all
 
 areg   roma_vignette  age_01_years girl  b_mat_c  b_gra_c  b_lit_c  b_dil_c  b_beh_c   if roma==0, absorb(class_id) 
 su roma_vignette if e(sample)==1
estadd scalar mean_depvar_c = r(mean)
 estadd local Class "Yes"
 est store bal_all_v
 
local varlist /// 
age_01_years girl  b_mat_c  b_gra_c  b_lit_c  b_dil_c  b_beh_c   ///
 
cd "$results"
#delimit;
	noisily esttab bal_* using "Balance.tex", 
	style(tex) stats(mean_depvar_c N r2 Class, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles("Roma deskmate (RD)"  "RD" "RD"  "RD"  "RD"  "RD"  "RD"  "RD" "Roma vignette")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate"
	age_01_years "Age (in 0.1 years)"
	girl  "Girl"
	roma "Roma"
	b_mat_c "Mathematics" 
	b_gra_c "Grammar" 
	b_lit_c "Literature"
	b_dil_c "Diligence"
	b_beh_c "Behavior"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr
	
	

* Add to table by hand
areg   dm_roma i.b_gr i.girl i.b_mat i.b_gra i.b_lit i.b_dil i.b_beh  if roma==0, absorb(class_id) allbase
testparm i.b_gr i.girl i.b_mat i.b_gra i.b_lit i.b_dil i.b_beh
/*

       F(105,  1966) =    1.09
            Prob > F =    0.2459


*/
areg   roma_vignette i.b_gr i.girl i.b_mat i.b_gra i.b_lit i.b_dil i.b_beh  if roma==0, absorb(class_id) allbase
testparm i.b_gr i.girl i.b_mat i.b_gra i.b_lit i.b_dil i.b_beh
/*

       F(102,  1831) =    0.87
            Prob > F =    0.8257
*/
* No need to present:
areg   dm_roma age_01_years girl  b_mat_c  b_gra_c  b_lit_c  b_dil_c  b_beh_c   if roma==0, absorb(class_id) 
testparm age_01_years girl  b_mat_c  b_gra_c  b_lit_c  b_dil_c  b_beh_c 


* TOST, added to text: 
areg  roma_friend dm_roma if roma==0, absorb(class_id) robust level(90)
areg  roma_friend dm_roma if roma==0, absorb(class_id) robust level(98)

areg   lend dm_roma roma_vignette roma_v_dm_roma if roma==0, absorb(class_id) robust level(90)

**** Table 3 ***************
set more off
areg  roma_friend dm_roma if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store a
areg  roma_friend dm_roma i.incl_miss_b_gr  incl_miss_girl  i.incl_miss_b_mat  i.incl_miss_b_gra  i.incl_miss_b_lit ///
 i.incl_miss_b_dil  i.incl_miss_b_beh miss_* if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "Yes"
  estadd local Base_int "No"
su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store b

set more off
areg   lend dm_roma roma_vignette roma_v_dm_roma if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
su lend if dm_roma==0 & roma_vignette==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store c
areg   lend dm_roma roma_vignette roma_v_dm_roma i.incl_miss_b_gr  incl_miss_girl  i.incl_miss_b_mat  i.incl_miss_b_gra  i.incl_miss_b_lit ///
 i.incl_miss_b_dil  i.incl_miss_b_beh miss_* if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "Yes"
  estadd local Base_int "No"
su lend if dm_roma==0 & roma_vignette==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store d


local varlist /// 
dm_roma roma_vignette roma_v_dm_roma ///
 
cd "$results"
#delimit;
	noisily esttab c d a b  using "Baseline_regressions.tex", 
	style(tex) stats(mean_depvar_c N r2 Class Base, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects" "Baseline Controls")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles("Lend to classmate"  "Lend to classmate"  "Roma friend"  "Roma friend"   )
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate"
		roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr
	

**** Table 4 ***************

	
	
areg  lend    srg_z  roma_vignette roma_vignette_srg_z if roma==0 , cluster(gr_school) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "No"
  su lend if  e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store ma 
areg  lend    srg_z  roma_vignette roma_vignette_srg_z    i.incl_miss_b_gr  incl_miss_girl  i.incl_miss_b_mat  i.incl_miss_b_gra  i.incl_miss_b_lit ///
 i.incl_miss_b_dil  i.incl_miss_b_beh miss_* if roma==0 , cluster(gr_school) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "Yes"
  su lend if  e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store mb
areg  roma_friend    srg_z    if roma==0 , cluster(gr_school) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "No"
  su roma_friend if e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store mc
areg  roma_friend    srg_z    i.incl_miss_b_gr  incl_miss_girl  i.incl_miss_b_mat  i.incl_miss_b_gra  i.incl_miss_b_lit ///
 i.incl_miss_b_dil  i.incl_miss_b_beh miss_* if roma==0 , cluster(gr_school) absorb(school_id) robust	
	 estadd local School "Yes"
 estadd local gp "Yes"
  su roma_friend if e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store md


	
	
local varlist /// 
   srg_z  roma_vignette roma_vignette_srg_z     ///
 
cd "$results"
#delimit;
	noisily esttab ma mb mc md  using "het_main.tex", 
	 style(tex) stats(mean_depvar_c N r2 School gp, labels("Mean outcome at average share" "No. of observations" "R-squared" "School fixed effects" "Baseline controls")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Lend"  "Lend"  "Friend"  "Friend")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate (RD)"
	roma_vignette "Roma vignette (RV)"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
	mrc "Majority Roma class"
	dm_roma_mrc  "Roma Deskmate*Majority Roma"
	srg_z "Share Roma in grade"
	dm_roma_srg_z "Roma Deskmate*share Roma"
	b_gpa_class "Average class GPA"
		dm_roma "Roma Deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma Deskmate*Roma vignette"
	mrc "Majority Roma class"
	dm_roma_mrc  "Roma Deskmate*Majority Roma"
	roma_vignette_mrc "RV*Majority Roma"
	roma_v_dm_roma_mrc "RD*RV*Majority Roma"
	srg_z "Share Roma in grade"
	dm_roma_srg_z "Roma Deskmate*share Roma"
		roma_vignette_srg_z "RV * Share Roma"
	roma_v_dm_roma_srg_z "RD*RV*Share Roma"
	b_gpa_class "Average class GPA"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	

	
	
			
****************** Additional analyses using main data (online appendices, footnotes, and checks) *************************

******** Table A1 *************
* Appendix Table with classes with high compliance only *

set more off
areg  roma_friend dm_roma if roma==0 & low_compliance==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store a1
set more off
areg   lend dm_roma roma_vignette roma_v_dm_roma if roma==0 & low_compliance==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
su lend if dm_roma==0 & roma_vignette==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store a2



	local varlist /// 
dm_roma roma_vignette roma_v_dm_roma   ///
 
cd "$results"
#delimit;
	noisily esttab a1 a2 using "low_compliance.tex", 
	style(tex) stats(mean_depvar_c N r2 Class Sample, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects" "Sample")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Roma friend"  "Lend to classmate" )
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr
	
******** Table A2 *************
*** Appendix without missing *********

	set more off
areg   roma_friend dm_roma i.b_gr  girl  i.b_mat  i.b_gra  i.b_lit ///
 i.b_dil  i.b_beh miss_* if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store a
areg   lend dm_roma roma_vignette roma_v_dm_roma i.b_gr  girl  i.b_mat  i.b_gra  i.b_lit ///
 i.b_dil  i.b_beh miss_* if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "Yes"
  estadd local Base_int "No"
su lend if dm_roma==0 & roma_vignette==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store b
	

local varlist /// 
dm_roma roma_vignette roma_v_dm_roma ///
 
cd "$results"
#delimit;
	noisily esttab a b using "not_miss.tex", 
	style(tex) stats(mean_depvar_c N r2 Class Base Base_int, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects" "Baseline Controls" "Baseline*Treatment")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Roma friend"  "Lend to classmate" )
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
	
******** Table A3 *************

	
			****************** Heterogeneity ego baseline and GPA **********************

	
 foreach var of varlist birth_z  girl  b_mat_c_z  b_gra_c_z  b_lit_c_z  b_dil_c_z  b_beh_c_z b_gpa_z {
areg  roma_friend  dm_roma `var' dm_roma_`var' if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store hetero_friend_z_`var'
areg  lend dm_roma roma_vignette roma_v_dm_roma `var' dm_roma_`var'  roma_vignette_`var' roma_v_dm_roma_`var' if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store hetero_lend_z_`var'
}

	local varlist /// 
dm_roma*   birth_z  girl  b_mat_c_z  b_gra_c_z  b_lit_c_z  b_dil_c_z  b_beh_c_z b_gpa_z ///

	cd "$results"
#delimit;
	noisily esttab hetero_friend_z_* using "heterogeneity_friend_z.tex", 
	style(tex) stats(mean_depvar_c N r2 Class, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects")  fmt(2 0 2)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Friend"  "Friend" "Friend"  "Friend" "Friend"  "Friend" "Friend" "Friend")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate (RD)"
	roma_vignette "Roma vignette (RV)"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
	birth_z "Age"
	girl  "Girl"
	b_mat_c_z "Mathematics" 
	b_gra_c_z "Grammar" 
	b_lit_c_z "Literature"
	b_dil_c_z "Diligence"
	b_beh_c_z "Behavior"
	b_gpa_z "GPA"
	dm_roma_birth_z "Roma deskmate*Age"
	roma_vignette_birth_z "RV*Age"
	roma_v_dm_roma_birth_z "RD*RV*Age"
	dm_roma_girl "Roma deskmate*Girl"
	roma_vignette_girl "RV*Girl"
	roma_v_dm_roma_girl "RD*RV*Girl"
	dm_roma_b_mat_c_z "Roma deskmate*Mathematics"
	roma_vignette_b_mat_c_z "RV*Mathematics"
	roma_v_dm_roma_b_mat_c_z "RD*RV*Mathematics"
		dm_roma_b_gra_c_z "Roma deskmate*Grammar"
	roma_vignette_b_gra_c_z "RV*Grammar"
	roma_v_dm_roma_b_gra_c_z "RD*RV*Grammar"
		dm_roma_b_lit_c_z "Roma deskmate*Literature"
	roma_vignette_b_lit_c_z "RV*Literature"
	roma_v_dm_roma_b_lit_c_z "RD*RV*Literature"
		dm_roma_b_dil_c_z "Roma deskmate*Diligence"
	roma_vignette_b_dil_c_z "RV*Diligence"
	roma_v_dm_roma_b_dil_c_z "RD*RV*Diligence"
		dm_roma_b_beh_c_z "Roma deskmate*Behavior"
	roma_vignette_b_beh_c_z "RV*Behavior"
	roma_v_dm_roma_b_beh_c_z "RD*RV*Behavior"
		dm_roma_b_gpa_z "Roma deskmate*GPA"
	roma_vignette_b_gpa_z "RV*GPA"
	roma_v_dm_roma_b_gpa_z "RD*RV*GPA"
				)
	prehead("\begin{tabular}{l*{15}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
	
******** Table A4 *************

	
	local varlist /// 
dm_roma*  roma_vignette roma_v_dm_roma roma_vignette_* roma_v_dm_roma_* birth_z  girl  b_mat_c_z  b_gra_c_z  b_lit_c_z  b_dil_c_z  b_beh_c_z b_gpa_z ///

	cd "$results"
#delimit;
	noisily esttab hetero_lend_z_* using "heterogeneity_lend_z.tex", 
	style(tex) stats(mean_depvar_c N r2 Class, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects")  fmt(2 0 2)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Lend" "Lend" "Lend" "Lend" "Lend" "Lend" "Lend" "Lend")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate (RD)"
	roma_vignette "Roma vignette (RV)"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
	birth_z "Age"
	girl  "Girl"
	b_mat_c_z "Mathematics" 
	b_gra_c_z "Grammar" 
	b_lit_c_z "Literature"
	b_dil_c_z "Diligence"
	b_beh_c_z "Behavior"
	b_gpa_z "GPA"
	dm_roma_birth_z "Roma deskmate*Age"
	roma_vignette_birth_z "RV*Age"
	roma_v_dm_roma_birth_z "RD*RV*Age"
	dm_roma_girl "Roma deskmate*Girl"
	roma_vignette_girl "RV*Girl"
	roma_v_dm_roma_girl "RD*RV*Girl"
	dm_roma_b_mat_c_z "Roma deskmate*Mathematics"
	roma_vignette_b_mat_c_z "RV*Mathematics"
	roma_v_dm_roma_b_mat_c_z "RD*RV*Mathematics"
		dm_roma_b_gra_c_z "Roma deskmate*Grammar"
	roma_vignette_b_gra_c_z "RV*Grammar"
	roma_v_dm_roma_b_gra_c_z "RD*RV*Grammar"
		dm_roma_b_lit_c_z "Roma deskmate*Literature"
	roma_vignette_b_lit_c_z "RV*Literature"
	roma_v_dm_roma_b_lit_c_z "RD*RV*Literature"
		dm_roma_b_dil_c_z "Roma deskmate*Diligence"
	roma_vignette_b_dil_c_z "RV*Diligence"
	roma_v_dm_roma_b_dil_c_z "RD*RV*Diligence"
		dm_roma_b_beh_c_z "Roma deskmate*Behavior"
	roma_vignette_b_beh_c_z "RV*Behavior"
	roma_v_dm_roma_b_beh_c_z "RD*RV*Behavior"
		dm_roma_b_gpa_z "Roma deskmate*GPA"
	roma_vignette_b_gpa_z "RV*GPA"
	roma_v_dm_roma_b_gpa_z "RD*RV*GPA"
			)
	prehead("\begin{tabular}{l*{15}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
******** Table A5 *************
 
		
	****************** Characteristics of DM **********************
	
 foreach var of varlist age_z  dm_girl  dm_b_mat_c_z  dm_b_gra_c_z  dm_b_lit_c_z  dm_b_dil_c_z  dm_b_beh_c_z dm_b_gpa_z {
areg  roma_friend  dm_roma `var' dm_roma_`var' if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store dm_h_friend_z_`var'
areg  lend dm_roma roma_vignette roma_v_dm_roma `var' dm_roma_`var'  roma_vignette_`var' roma_v_dm_roma_`var' if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store dm_h_lend_z_`var'
}

	local varlist /// 
dm_roma*   age_z  dm_girl  dm_b_mat_c_z  dm_b_gra_c_z  dm_b_lit_c_z  dm_b_dil_c_z  dm_b_beh_c_z dm_b_gpa_z ///

	cd "$results"
#delimit;
	noisily esttab dm_h_friend_z_* using "dm_heterogeneity_friend_z.tex", 
	style(tex) stats(mean_depvar_c N r2 Class, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects")  fmt(2 0 2)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Friend"  "Friend" "Friend"  "Friend" "Friend"  "Friend" "Friend" "Friend")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate (RD)"
	roma_vignette "Roma vignette (RV)"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
	age_z "Age"
	dm_girl  "Girl"
	dm_b_mat_c_z "Mathematics" 
	dm_b_gra_c_z "Grammar" 
	dm_b_lit_c_z "Literature"
	dm_b_dil_c_z "Diligence"
	dm_b_beh_c_z "Behavior"
	dm_b_gpa_z "GPA"
	dm_roma_age_z "Roma deskmate*Age"
	roma_vignette_age_z "RV*Age"
	roma_v_dm_roma_age_z "RD*RV*Age"
	dm_roma_dm_girl "Roma deskmate*Girl"
	roma_vignette_dm_girl "RV*Girl"
	roma_v_dm_roma_dm_girl "RD*RV*Girl"
	dm_roma_dm_b_mat_c_z "Roma deskmate*Mathematics"
	roma_vignette_dm_b_mat_c_z "RV*Mathematics"
	roma_v_dm_roma_dm_b_mat_c_z "RD*RV*Mathematics"
		dm_roma_dm_b_gra_c_z "Roma deskmate*Grammar"
	roma_vignette_dm_b_gra_c_z "RV*Grammar"
	roma_v_dm_roma_dm_b_gra_c_z "RD*RV*Grammar"
		dm_roma_dm_b_lit_c_z "Roma deskmate*Literature"
	roma_vignette_dm_b_lit_c_z "RV*Literature"
	roma_v_dm_roma_dm_b_lit_c_z "RD*RV*Literature"
		dm_roma_dm_b_dil_c_z "Roma deskmate*Diligence"
	roma_vignette_dm_b_dil_c_z "RV*Diligence"
	roma_v_dm_roma_dm_b_dil_c_z "RD*RV*Diligence"
		dm_roma_dm_b_beh_c_z "Roma deskmate*Behavior"
	roma_vignette_dm_b_beh_c_z "RV*Behavior"
	roma_v_dm_roma_dm_b_beh_c_z "RD*RV*Behavior"
		dm_roma_dm_b_gpa_z "Roma deskmate*GPA"
	roma_vignette_dm_b_gpa_z "RV*GPA"
	roma_v_dm_roma_dm_b_gpa_z "RD*RV*GPA"
				)
	prehead("\begin{tabular}{l*{15}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
	
******** Table A6 *************

	
	local varlist /// 
dm_roma*  roma_vignette roma_v_dm_roma roma_vignette_* roma_v_dm_roma_* age_z  dm_girl  ///
dm_b_mat_c_z  dm_b_gra_c_z  dm_b_lit_c_z  dm_b_dil_c_z  dm_b_beh_c_z dm_b_gpa_z ///

	cd "$results"
#delimit;
	noisily esttab dm_h_lend_z_* using "dm_heterogeneity_lend_z.tex", 
	style(tex) stats(mean_depvar_c N r2 Class, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects")  fmt(2 0 2)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Lend" "Lend" "Lend" "Lend" "Lend" "Lend" "Lend" "Lend")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate (RD)"
	roma_vignette "Roma vignette (RV)"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
	age_z "Age"
	dm_girl  "Girl"
	dm_b_mat_c_z "Mathematics" 
	dm_b_gra_c_z "Grammar" 
	dm_b_lit_c_z "Literature"
	dm_b_dil_c_z "Diligence"
	dm_b_beh_c_z "Behavior"
	dm_b_gpa_z "GPA"
	dm_roma_age_z "Roma deskmate*Age"
	roma_vignette_age_z "RV*Age"
	roma_v_dm_roma_age_z "RD*RV*Age"
	dm_roma_dm_girl "Roma deskmate*Girl"
	roma_vignette_dm_girl "RV*Girl"
	roma_v_dm_roma_dm_girl "RD*RV*Girl"
	dm_roma_dm_b_mat_c_z "Roma deskmate*Mathematics"
	roma_vignette_dm_b_mat_c_z "RV*Mathematics"
	roma_v_dm_roma_dm_b_mat_c_z "RD*RV*Mathematics"
		dm_roma_dm_b_gra_c_z "Roma deskmate*Grammar"
	roma_vignette_dm_b_gra_c_z "RV*Grammar"
	roma_v_dm_roma_dm_b_gra_c_z "RD*RV*Grammar"
		dm_roma_dm_b_lit_c_z "Roma deskmate*Literature"
	roma_vignette_dm_b_lit_c_z "RV*Literature"
	roma_v_dm_roma_dm_b_lit_c_z "RD*RV*Literature"
		dm_roma_dm_b_dil_c_z "Roma deskmate*Diligence"
	roma_vignette_dm_b_dil_c_z "RV*Diligence"
	roma_v_dm_roma_dm_b_dil_c_z "RD*RV*Diligence"
		dm_roma_dm_b_beh_c_z "Roma deskmate*Behavior"
	roma_vignette_dm_b_beh_c_z "RV*Behavior"
	roma_v_dm_roma_dm_b_beh_c_z "RD*RV*Behavior"
		dm_roma_dm_b_gpa_z "Roma deskmate*GPA"
	roma_vignette_dm_b_gpa_z "RV*GPA"
	roma_v_dm_roma_dm_b_gpa_z "RD*RV*GPA"
			)
	prehead("\begin{tabular}{l*{15}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
	
******** Table A7 *************

	
		
		****************** Heterogeneity same sex **********************
		set more off
areg  roma_friend dm_roma if roma==0 &  same_s==1, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  estadd local Sa "Same sex"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store a
		set more off
areg  roma_friend dm_roma if roma==0 &  same_s==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  estadd local Sa "Different sex"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store b
	set more off
areg   lend dm_roma roma_vignette roma_v_dm_roma if roma==0 &  same_s==1, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  estadd local Sa "Same sex"
su lend if dm_roma==0 & roma_vignette==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store c
	set more off
areg   lend dm_roma roma_vignette roma_v_dm_roma if roma==0 &  same_s==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  estadd local Sa "Different sex"
su lend if dm_roma==0 & roma_vignette==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store d

local varlist /// 
dm_roma roma_vignette roma_v_dm_roma ///
 
cd "$results"
#delimit;
	noisily esttab a b c d using "same_sex.tex", 
	style(tex) stats(mean_depvar_c N r2 Class Sa, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects"  "Deskmate sex")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Roma friend"  "Roma friend"  "Lend to classmate" "Lend to classmate" )
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
******** Table A8 *************
* Appendix table following preplan:

areg  lend_roma dm_roma  lend_dm dm_roma_lend_dm if roma==0, absorb(class_id) robust
estadd local Class "Yes"
estadd local Base "No"
estadd local Base_int "No"
su lend_roma if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store aa


	local varlist /// 
dm_roma  lend_dm dm_roma_lend_dm  ///

	cd "$results"
#delimit;
	noisily esttab aa using "lend_roma_lend_dm.tex", 
	style(tex) stats(mean_depvar_c N r2 Class, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects")  fmt(2 0 2)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Lend to Roma")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate"
	roma_vignette "Roma vignette (RV)"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
	lend_dm "Lend to Deskmate"
	dm_roma_lend_dm "Roma deskmate*Lend to Deskmate"
				)
	prehead("\begin{tabular}{l*{15}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
				
******** Table A9 *************
		
	
reg  roma_friend dm_roma mrc_grade dm_roma_mrc_grade if roma==0 , cluster(grade_id) robust
 estadd local School "No"
 estadd local gp "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store a
areg  roma_friend dm_roma mrc_grade dm_roma_mrc_grade if roma==0 , cluster(grade_id) absorb(school_id) robust		
 estadd local School "Yes"
 estadd local gp "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store b
reg  roma_friend dm_roma src_grade_z dm_roma_src_grade_z if roma==0 , cluster(grade_id) robust
 estadd local School "No"
 estadd local gp "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store c
areg  roma_friend dm_roma src_grade_z dm_roma_src_grade_z if roma==0 , cluster(grade_id) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store d
areg  roma_friend dm_roma src_grade_z dm_roma_src_grade_z b_gpa_grade if roma==0 , cluster(grade_id) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "Yes"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store e	
	reg  lend dm_roma roma_vignette roma_v_dm_roma  src_grade_z dm_roma_src_grade_z roma_vignette_src_grade_z roma_v_dm_roma_src_grade_z if roma==0 , cluster(grade_id) robust
 estadd local School "No"
 estadd local gp "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store c2
areg  lend dm_roma roma_vignette roma_v_dm_roma  src_grade_z dm_roma_src_grade_z roma_vignette_src_grade_z roma_v_dm_roma_src_grade_z if roma==0 , cluster(grade_id) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store d2
areg  lend dm_roma roma_vignette roma_v_dm_roma  src_grade_z dm_roma_src_grade_z roma_vignette_src_grade_z roma_v_dm_roma_src_grade_z b_gpa_grade  if roma==0 , cluster(grade_id) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "Yes"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store e2



local varlist /// 
dm_roma    src_grade_z dm_roma_src_grade_z  ///
 dm_roma roma_vignette roma_v_dm_roma   src_grade_z dm_roma_src_grade_z ///
 roma_vignette_src_grade_z roma_v_dm_roma_src_grade_z b_gpa_grade ///
 
cd "$results"
#delimit;
	noisily esttab c2 d2 e2 c d e  using "context_combo_grade.tex", 
	style(tex) stats(mean_depvar_c N r2 School gp, labels("Mean outcome for controls" "No. of observations" "R-squared" "School F.E." "Average grade GPA")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Lend"  "Lend"  "Lend" "Friend"  "Friend"  "Friend")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate (RD)"
	roma_vignette "Roma vignette (RV)"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
	mrc_grade "Majority Roma grade"
	dm_roma_mrc_grade  "Roma deskmate*Majority Roma"
	src_grade_z "Share Roma in grade"
	dm_roma_src_grade_z "Roma deskmate*share Roma"
	b_gpa_grade "Average grade GPA"
		dm_roma "Roma deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
	mrc_grade "Majority Roma grade"
	dm_roma_mrc_grade  "Roma deskmate*Majority Roma"
	roma_vignette_mrc_grade "RV*Majority Roma"
	roma_v_dm_roma_mrc_grade "RD*RV*Majority Roma"
	src_grade_z "Share Roma in grade"
	dm_roma_src_grade_z "Roma deskmate*share Roma"
		roma_vignette_src_grade_z "RV*Share Roma"
	roma_v_dm_roma_src_grade_z "RD*RV*Share Roma"
	b_gpa_grade "Average grade GPA"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
	
******** Table A10 *************

	
		****************** Heterogeneity Roma majority **********************
		set more off
areg  roma_friend dm_roma if roma==0 &  maj_roma_class==1, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  estadd local Sa "Majority Roma"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store a
		set more off
areg  roma_friend dm_roma if roma==0 &  maj_roma_class==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  estadd local Sa "Majority non-Roma"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store b
	set more off
areg   lend dm_roma roma_vignette roma_v_dm_roma if roma==0 &  maj_roma_class==1, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
   estadd local Sa "Majority Roma"
su lend if dm_roma==0 & roma_vignette==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store c
	set more off
areg   lend dm_roma roma_vignette roma_v_dm_roma if roma==0 &  maj_roma_class==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  estadd local Sa "Majority non-Roma"
su lend if dm_roma==0 & roma_vignette==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store d

local varlist /// 
dm_roma roma_vignette roma_v_dm_roma ///
 
cd "$results"
#delimit;
	noisily esttab a b c d using "maj_roma.tex", 
	style(tex) stats(mean_depvar_c N r2 Class Sa, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects"  "Sample")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Roma friend"  "Roma friend"  "Lend to classmate" "Lend to classmate" )
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
******** Table A11 *************

	
		areg  lend    src_z   roma_vignette roma_vignette_src_z  if roma==0 , cluster(gr_school) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "No"
    estadd local base "No"
  su lend if  e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store mac 
areg  lend    src_z   roma_vignette roma_vignette_src_z   b_gpa_class if roma==0 , cluster(gr_school) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "Yes"
    estadd local base "No"
  su lend if  e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store mbc
areg  lend    src_z   roma_vignette roma_vignette_src_z   b_gpa_class i.incl_miss_b_gr  incl_miss_girl  i.incl_miss_b_mat  i.incl_miss_b_gra  i.incl_miss_b_lit ///
 i.incl_miss_b_dil  i.incl_miss_b_beh miss_* if roma==0 , cluster(gr_school) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "Yes"
   estadd local base "Yes"
  su lend if  e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store mlcontrol
areg  roma_friend    src_z    if roma==0 , cluster(gr_school) absorb(school_id) robust	
 estadd local School "Yes"
 estadd local gp "No"
    estadd local base "No"
  su roma_friend if e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store mcc
areg  roma_friend    src_z   b_gpa_class if roma==0 , cluster(gr_school) absorb(school_id) robust	
	 estadd local School "Yes"
 estadd local gp "Yes"
   estadd local base "No"
  su roma_friend if e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store mdc
areg  roma_friend    src_z   b_gpa_class i.incl_miss_b_gr  incl_miss_girl  i.incl_miss_b_mat  i.incl_miss_b_gra  i.incl_miss_b_lit ///
 i.incl_miss_b_dil  i.incl_miss_b_beh miss_* if roma==0 , cluster(gr_school) absorb(school_id) robust	
	 estadd local School "Yes"
 estadd local gp "Yes"
  estadd local base "Yes"
  su roma_friend if e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store mfcontrol



	
	
local varlist /// 
src_z   roma_vignette roma_vignette_src_z    ///
 
cd "$results"
#delimit;
	noisily esttab  mbc mlcontrol mdc mfcontrol using "het_main_class.tex", 
	 style(tex) stats(mean_depvar_c N r2 School gp base, labels("Mean dep. var at average share" "No. of observations" "R-squared" "School F.E." "Average class GPA" "Baseline controls")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Lend"  "Lend"  "Friend"  "Friend")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma Deskmate (RD)"
	roma_vignette "Roma vignette (RV)"
	roma_v_dm_roma "Roma Deskmate*Roma vignette"
	mrc "Majority Roma class"
	dm_roma_mrc  "Roma Deskmate*Majority Roma"
	src_z "Share Roma in class"
	dm_roma_src_z "Roma Deskmate*share Roma"
	b_gpa_class "Average class GPA"
		dm_roma "Roma Deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma Deskmate*Roma vignette"
	mrc "Majority Roma class"
	dm_roma_mrc  "Roma Deskmate*Majority Roma"
	roma_vignette_mrc "RV*Majority Roma"
	roma_v_dm_roma_mrc "RD*RV*Majority Roma"
	src_z "Share Roma in grade"
	dm_roma_src_z "Roma Deskmate*share Roma"
		roma_vignette_src_z "RV*Share Roma"
	roma_v_dm_roma_src_z "RD*RV*Share Roma"
	b_gpa_class "Average class GPA"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
	
******** Table A12 *************

	
		*************** Heterogeneity share roma ****************
	

areg  lend dm_roma roma_vignette roma_v_dm_roma   dm_roma_src_z roma_vignette_src_z roma_v_dm_roma_src_z i.incl_miss_b_gr  incl_miss_girl  i.incl_miss_b_mat  i.incl_miss_b_gra  i.incl_miss_b_lit ///
 i.incl_miss_b_dil  i.incl_miss_b_beh miss_*  if roma==0 , cluster(gr_school) absorb(class_id) robust	
 estadd local School "Yes"
  estadd local Base "No"
  su lend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store b
areg  lend dm_roma roma_vignette roma_v_dm_roma   dm_roma_src_z roma_vignette_src_z roma_v_dm_roma_src_z if roma==0 , cluster(gr_school) absorb(class_id) robust	
 estadd local School "Yes"
  estadd local Base "Yes"
  su lend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store b2
areg  roma_friend dm_roma  dm_roma_src_z  if roma==0 , cluster(class_id) absorb(class_id) robust	
 estadd local School "Yes"
  estadd local Base "No"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store c
areg  roma_friend dm_roma  dm_roma_src_z  i.incl_miss_b_gr  incl_miss_girl  i.incl_miss_b_mat  i.incl_miss_b_gra  i.incl_miss_b_lit ///
 i.incl_miss_b_dil  i.incl_miss_b_beh miss_* if roma==0 , cluster(class_id) absorb(class_id) robust	
 estadd local School "Yes"
  estadd local Base "Yes"
  su roma_friend if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store c2



 

local varlist /// 
dm_roma roma_vignette roma_v_dm_roma   dm_roma_src_z roma_vignette_src_z roma_v_dm_roma_src_z  ///
 
cd "$results"
#delimit;
	noisily esttab b b2 c c2  using "het_main_new.tex", 
	style(tex) stats(mean_depvar_c N r2 School Base, labels("Mean dep. var in C group" "No. of observations" "R-squared" "Class F.E." "Baseline Controls")  fmt(2 0 2 0)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Lend" "Lend"  "Friend" "Friend")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma Deskmate (RD)"
	roma_vignette "Roma vignette (RV)"
	roma_v_dm_roma "Roma Deskmate*Roma vignette"
	mrc "Majority Roma class"
	dm_roma_src  "Roma Deskmate*Share Roma"
	srg_z "Share Roma in grade"
	dm_roma_src_z "Roma Deskmate*share Roma"
	b_gpa_class "Average class GPA"
	roma_vignette_src_z "Roma Vignette*Share Roma"
		dm_roma "Roma Deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma Deskmate*Roma vignette"
	mrc "Majority Roma class"
	dm_roma_src  "Roma Deskmate*Share Roma"
	roma_vignette_src "RV*Majority Roma"
	roma_v_dm_roma_src "RD*RV*Share Roma"
	srg_z "Share Roma in grade"
	dm_roma_srg_z "Roma Deskmate*share Roma"
		roma_vignette_srg_z "RV*Share Roma"
	roma_v_dm_roma_src_z "RD*RV*Share Roma"
	b_gpa_class "Average class GPA"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	

******** Table A13 *************
* Uses other data, see below 
	
******** Table A14 *************

		****************** Other outcomes **********************
* lend_roma lend_non_roma
 foreach var of varlist number_roma_f  rf_class dm_best  {
areg  `var' dm_roma if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  su `var' if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store othera_`var'
}

local varlist /// 
dm_roma  ///

* "Lend to Roma" "Lend to non-Roma" 
	cd "$results"
#delimit;
	noisily esttab othera_* using "other_outcomes1.tex", 
	style(tex) stats(mean_depvar_c N r2 Class, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects")  fmt(2 0 2)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Number of Roma friends" "Roma among best friends" "Deskmate among best friends")
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	

	
******** Table A15 *************

	
 foreach var of varlist  lend_dm liked_dm  {
areg  `var' dm_roma if roma==0, absorb(class_id) robust
 estadd local Class "Yes"
 estadd local Base "No"
  estadd local Base_int "No"
  su `var' if dm_roma==0 & e(sample)==1
estadd scalar mean_depvar_c = r(mean)
est store otherb_`var'
}

local varlist /// 
dm_roma  ///

* "Lend to Roma" "Lend to non-Roma" 
	cd "$results"
#delimit;
	noisily esttab otherb_* using "other_outcomes2.tex", 
	style(tex) stats(mean_depvar_c N r2 Class, labels("Mean outcome for controls" "No. of observations" "R-squared" "Class fixed effects")  fmt(2 0 2)) 
	starlevels(* 0.10 ** 0.05 *** 0.01) b(a2) se 
mtitles( "Lend to deskmate" "Liked sitting next to deskmate" )
		keep(`varlist') order(`varlist')
		varlabels(
	dm_roma "Roma deskmate"
	roma_vignette "Roma vignette"
	roma_v_dm_roma "Roma deskmate * Roma vignette"
			)
	prehead("\begin{tabular}{l*{12}{l}}" "\hline") posthead(\hline)
	postfoot("\hline" "\end{tabular}")
	replace;
	#delimit cr	
	
	

* Some numbers mentioned in text

codebook class_id if e(sample)==1
areg   dm_roma , absorb(class_id) robust
codebook class_id school_id if e(sample)==1
codebook class_id school_id id if e(sample)==1 & roma==0

codebook class_id school_id id
codebook class_id school_id id if roma==0

* In class measure (nominate 5):
* No effect either, but 22 percent have roma friends.
su rf_class if roma!=1
areg  rf_class dm_roma if roma==0, absorb(class_id) robust

	
su dm_roma if roma==0
su lend if  dm_roma!=. & roma==0
su roma_friend if  dm_roma!=. & roma==0

* Attrition: Add to text. 
gen attrit_lend=1 if lend==. & dm_roma!=. & roma==0
replace attrit_lend=0 if lend!=. & dm_roma!=. & roma==0

gen attrit_friend=1 if roma_friend==. & dm_roma!=. & roma==0
replace attrit_friend=0 if roma_friend!=. & dm_roma!=. & roma==0

areg   attrit_lend dm_roma  if roma==0, absorb(class_id) 
areg   attrit_friend dm_roma  if roma==0, absorb(class_id) 

* Not correlated with treatment:
reg  low_compliance dm_roma  if roma==0,  cluster(class_id)
* Not differential treatment effects:
reg  roma_friend dm_roma##low_compliance if roma==0,  cluster(class_id) robust
reg   lend dm_roma##low_compliance roma_vignette##low_compliance roma_v_dm_roma##low_compliance if roma==0,  cluster(class_id) robust
* Nor if we do ind level (but note, likely post treatment)
areg  roma_friend dm_roma##change_dm_status if roma==0, absorb(class_id) robust
areg   lend dm_roma##change_dm_statu roma_vignette##change_dm_statu roma_v_dm_roma##change_dm_statu if roma==0, absorb(class_id) robust






**************** Appendix results using other data **********************	





* Figures A1-A3


******* Figures A1 and A2 use survey data from 2021 
clear all
global DATA "C:\Users\andrkot\Dropbox (Frischsenteret)\Hungarian peers\Replication files AJS\Data\"

use "$DATA\Teacher_survey_2021_Summer.dta" , clear



* Figure A1 
label define t12 1 "Not at all" 2 " " 3 "Somewhat" 4 " " 5 "To a great extent", replace
label value Q12_1 t12
label value Q12_2 t12
label value Q12_3 t12
label value Q12_4 t12



		

twoway 	(histogram Q12_1,   percent gap(10) xlab(,valuelabel labsize(medlarge)) ylab(,angle(0) labsize(medlarge)) xtitle("") ytitle("Percent") discrete color("252 221 9%50")  graphregion(color(white))) 	///
		,  title("To what extent do deskmates affect academic achievement?") name(hist_d12_1, replace)
		graph export "$results/hist_d12_1.pdf", as(pdf) replace	
		graph export "$results/hist_d12_1.png", as(png) replace	
twoway 	(histogram Q12_2,   percent gap(10) xlab(,valuelabel labsize(medlarge)) ylab(,angle(0) labsize(medlarge)) xtitle("") ytitle("Percent") discrete color("252 221 9%50")  graphregion(color(white))) 	///
		,  title("To what extent do deskmates affect friendship formation?") name(hist_d12_2, replace)
		graph export "$results/hist_d12_2.pdf", as(pdf) replace	
		graph export "$results/hist_d12_2.png", as(png) replace	
twoway 	(histogram Q12_3,   percent gap(10) xlab(,valuelabel labsize(medlarge)) ylab(,angle(0) labsize(medlarge)) xtitle("") ytitle("Percent") discrete color("252 221 9%50")  graphregion(color(white))) 	///
		,  title("To what extent do deskmates affect school behavior?") name(hist_d12_3, replace)
		graph export "$results/hist_d12_3.pdf", as(pdf) replace	
		graph export "$results/hist_d12_3.png", as(png) replace	
twoway 	(histogram Q12_4,   percent gap(10) xlab(,valuelabel labsize(medlarge)) ylab(,angle(0) labsize(medlarge)) xtitle("") ytitle("Percent") discrete color("252 221 9%50")  graphregion(color(white))) 	///
		,  title("To what extent do deskmates affect diligence?") name(hist_d12_4, replace)
		graph export "$results/hist_d12_4.pdf", as(pdf) replace	
		graph export "$results/hist_d12_4.png", as(png) replace		
	
grc1leg2 hist_d12_1 hist_d12_2 hist_d12_3 hist_d12_4, graphregion(color(white)) imargin(0) ycommon row(2) 
graph display, xsize(12) ysize(4) scale(1.2)		
graph export "$results/hist_d12.pdf", as(pdf) replace		
graph export "$results/hist_d12.png", as(png) replace	



* Figure A2
*"Forty percent of homeroom teachers who employ a seating chart specifically place strong and weak students together. "

des Q11*
tab Q11_2

recode Q11_2 (2 3=0), gen(strong)
tab strong
label define t11 0 "No" 1 "Yes" , replace
label value strong t11


twoway 	(histogram strong,   percent gap(10) xlab(,valuelabel labsize(medlarge)) ylab(,angle(0) labsize(medlarge)) xtitle("") ytitle("Percent") discrete color("252 221 9%50") xlabel(0 1) graphregion(color(white))) 	///
		,  title("Teacher places strong and weak students together") 
		graph export "$results/hist_strong.pdf", as(pdf) replace	
		graph export "$results/hist_strong.png", as(png) replace	
		
		
****** Figure A3 and Table A13 ******
* Uses survey data from 2022



clear all
global DATA "C:\Users\andrkot\Dropbox (Frischsenteret)\Hungarian peers\Replication files AJS\Data\"

use "$DATA\Teacher_survey_2022_February.dta" , clear


********* Table A13 **************	

regress Q5_1  i.b4.teltip TE02807 m_stat o_stat , cluster (x)
outreg2 using x.xls, label dec(3)   alpha(0.01,0.05,0.1)  symbol(**, *, +)  replace


foreach y in  Q5_2 Q5_3 Q5_4 Q5_5 Q5_6 Q5_7 Q5_8 Q5_9 {
	regress `y'  i.b4.teltip TE02807 m_stat o_stat  , cluster (x)
	outreg2 using x.xls, label dec(3)   alpha(0.01,0.05,0.1)  symbol(**, *, +)  append
}

shellout using x.xls

****** Figure A3 **************
*95% of teachers reported weekly activities, and 61% reported daily activities among deskmates

foreach i in Q5_1 Q5_2 Q5_3 Q5_4 Q5_5 Q5_6 Q5_7 Q5_8 Q5_9 {
	recode `i' (1 = 1) (2 3 4 5 = 0) (.=.), ge(`i'_d)
	recode `i' (1 2 = 1) (3 4 5 = 0) (.=.), ge(`i'_w)
}


egen dm_d = rowtotal(Q5_1_d Q5_2_d Q5_3_d Q5_4_d Q5_5_d Q5_6_d Q5_7_d Q5_8_d Q5_9_d)
egen dm_w = rowtotal(Q5_1_w Q5_2_w Q5_3_w Q5_4_w Q5_5_w Q5_6_w Q5_7_w Q5_8_w Q5_9_w)
* ssc install fre
fre dm_w dm_d

*The three most common deskmate activities were helping each other learn, working together, and developing social skills. Nearly 80% of teachers reported facilitating each of these activities at least every week




foreach i in Q5_1 Q5_2 Q5_3 Q5_4 Q5_5 Q5_6 Q5_7 Q5_8 Q5_9 {
tabulate `i', matcell(freq)
matrix list freq
putexcel set `i'.xls, replace
putexcel A1=("`i'")  
putexcel A2=matrix(freq/r(N))
}

foreach i in Q5_1 Q5_2 Q5_3 Q5_4 Q5_5 Q5_6 Q5_7 Q5_8 Q5_9 {
import excel using "$DATA\`i'.xls", firstrow clear
save "$DATA\`i'.dta", replace
}

use "$DATA\Q5_1.dta", clear
foreach i in   Q5_2 Q5_3 Q5_4 Q5_5 Q5_6 Q5_7 Q5_8 Q5_9 {
merge 1:1 _n using `i'.dta , nogen
}

xpose, varname   clear

egen var = fill (1 2 3 4 5 6 7 8 9)
label define var ///
1 "work together"  ///
2 "correct each other's work" ///
3 "explain exercise/course material" ///
4 "discuss issues/problems" ///
5 "help each other to learn" ///
6 "play out situations" ///
7 "do homework together" ///
8 "correct each other's homework" ///
9 "develop social skills"

label value var var

gen v = v1 + v2
gsort -v
gen r = _n
sort r, stable



rename (v1 v2 v3 v4 v5) (Daily Weekly Monthly Bimonthly Never)


graph hbar Daily Weekly Monthly Bimonthly Never, over(var, label(labsize(small)) sort(r))  stack ///
bar(1, bcolor(black)) bar(2, bcolor(gs4)) bar(3, bcolor(gs8)) bar(4, bcolor(gs10)) bar(5, bcolor(gs12)) ///
ylabel( 0 "0%" .1 "10%" .2 "20%" .3 "30%"  .4 "40%"  .5 "50%"  .6 "60%"  .7 "70%" .8 "80%" .9 "90%" 1 "100%" , labsize(small) nogrid angle(0) grid glwidth(vthin) glcolor(gs12) glpattern(longdash)) ///
graphregion(lcolor(white)fcolor(white)) ///
legend(col(2) label(1 "Almost every lesson") label(2 "At least weekly") label(3 "Monthly") label(4 "Less often than monthly") label(5 "Never") size(small) ) ///
yline(.5 , lcolor(black)) ///
note(N = 656)


	

	

	

	
